﻿#pragma once

#include <chrono>
#include <cstdint>

namespace asycipc {

class HeartbeatMonitor {
 public:
  HeartbeatMonitor(std::chrono::milliseconds interval,
                   uint32_t allowed_miss,
                   std::chrono::steady_clock::time_point now =
                       std::chrono::steady_clock::now());

  bool ShouldSend(std::chrono::steady_clock::time_point now) const;
  void MarkSent(std::chrono::steady_clock::time_point now);
  void MarkReceived(std::chrono::steady_clock::time_point now);
  bool IsExpired(std::chrono::steady_clock::time_point now) const;

  std::chrono::milliseconds interval() const noexcept { return interval_; }
  uint32_t allowed_miss() const noexcept { return allowed_miss_; }

 private:
  std::chrono::milliseconds interval_;
  uint32_t allowed_miss_;
  std::chrono::steady_clock::time_point last_send_;
  std::chrono::steady_clock::time_point last_receive_;
};

}  // namespace asycipc
